﻿using System;
using Entity;
using BLL;
using BLL.Access;
using System.Data;

namespace Web.Pages.YongShui._13.LAIHANGL
{
    public partial class YuDuTZKJDJ : BasePage
    {
        #region 属性
        public DataTable MyDataSource
        {
            get { return ViewState["DataSource"] == null ? null : ViewState["DataSource"] as DataTable; }
            set { ViewState["DataSource"] = value; }
        }
        #endregion

        #region 变量
        YS_LH_YUEDUYHXXProcess BllYue = new YS_LH_YUEDUYHXXProcess();
        SpHelper _sphelper = new SpHelper();
        SYS_LOGBLL BllLog = new SYS_LOGBLL();
        #endregion

        #region 事件


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitControl();
            }
            ASPxGridViewList.DataSource = this.MyDataSource;
            ASPxGridViewList.DataBind();
        }

        protected void DrpSite_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindData();
        }
        protected void PostBack_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {
            e.Result = e.Parameter;
            if (e.Parameter == "yonghuh")
            {
                e.Result = e.Parameter + "|" + YONGHUXX();
            }
        }

        #endregion

        #region 方法

        private void InitControl()
        {

            string zhandianBH = BusinessTool.GETZHANDIAN(LoginInfo.CurrentUser.UserID);
            BLL.YS_JX_ZHANDIANBLL bllT = new YS_JX_ZHANDIANBLL();
            DataTable rs = bllT.SelectYS_JX_ZHANDIANsDynamic(" S_ZHANDIANBH='" + zhandianBH + "' or '00'='" + zhandianBH + "'", null);
            foreach (DataRow dr in rs.Rows)
            {
                dr["S_ZHANDIANMC"] = dr["S_ZHANDIANBH"].ToString() + "-" + dr["S_ZHANDIANMC"].ToString();
            }

            this.DrpSite.DataSource = rs;
            this.DrpSite.TextField = "S_ZHANDIANMC";
            this.DrpSite.ValueField = "S_ZHANDIANBH";
            this.DrpSite.DataBind();


            if (zhandianBH == "00")
            {
                //DrpSite.Items.Insert(0, new DevExpress.Web.ListEditItem(" ", "-1"));
            }
            this.DrpSite.SelectedIndex = 0;
            BindData();
        }
        private void BindData()
        {

            int ym = BusinessTool.GetWaterYM();
            int y = Convert.ToInt32(ym.ToString().Substring(0, 4));
            int m = Convert.ToInt32(ym.ToString().Substring(4, 2));
            DataSet ds = _sphelper.P_YS_LH_JH_YONGHUYSSJ(DrpSite.Value.ToString(), y, m,LoginInfo.CurrentUser.UserID);
            this.MyDataSource = ds.Tables[0];
            ASPxGridViewList.DataSource = this.MyDataSource;
            ASPxGridViewList.DataBind();

            TotalSpan.InnerText = "共" + (MyDataSource != null ? MyDataSource.Rows.Count.ToString() : "0") + "条";
        }
        public string YONGHUXX()
        {
            string msg = "";
            if (YongHuHList.Contains("yonghuh") && YongHuHList.Get("yonghuh").ToString().Trim().Length > 0)
            {
                int ym = BusinessTool.GetWaterYM();
                int y = Convert.ToInt32(ym.ToString().Substring(0, 4));
                int m = Convert.ToInt32(ym.ToString().Substring(4, 2));

                string where = "S_YONGHUH='{0}' and I_Y={1} and I_M={1} and i_jlzt=0";
                where = string.Format(where, YongHuHList.Get("yonghuh"), y, m);
                DataTable dt = BllYue.SelectYS_LH_YUEDUYHXXesDynamic(where, "", null);

                if (dt.Rows.Count > 0)
                {
                    msg = "-1|用户已操作完成，无法再次操作";
                }
                else
                {
                    YS_LH_YUEDUYHXX yhxx = new YS_LH_YUEDUYHXX();
                    yhxx.D_CAOZURQ = BusinessTool.GetDataBaseDateTime();
                    yhxx.I_JLZT = 0;
                    yhxx.I_M = m;
                    yhxx.I_SHIFOUQR = 1;
                    yhxx.I_Y = y;
                    yhxx.ID = 0;
                    yhxx.S_BEIZHU = "";
                    yhxx.S_CAOZUOR = LoginInfo.CurrentUser.Account;
                    yhxx.S_YONGHUH = YongHuHList.Get("yonghuh").ToString();
                    BllLog.Log<YS_LH_YUEDUYHXX>(LoginInfo.CurrentUser, "来函月调整-来函确认", OperateState.Add, yhxx, null);
                    BllYue.InsertOrUpdate(yhxx);
                    if (yhxx.ID > 0)
                    {
                        msg = "1|操作成功";
                        
                    }
                    else
                    {
                        msg = "-1|操作失败";
                    }
                }

            }
            return msg;
        }

        public string GetInput(string yonghuh, string qr)
        {
            string input = "<input type=\"checkbox\" onclick=\"Check(this)\" value=\"{0}\"  {1} />";
            if (qr == "1")
            {
                input = string.Format(input, yonghuh, " disabled=\"disabled\"  checked=\"checked\" ");
            }
            else
            {
                input = string.Format(input, yonghuh, " ");
            }

            return input;
        }

        public string GetCaoZuo(string yonghuh,string type,string sftj)
        {
            string html = "";

            if (sftj != "0")
            {
                html = "已登记";
            }
            else
            {
                if (type == "0")
                {
                    html = "未确认";
                }
                else if (type == "1")
                {
                    html = "<a href=\"javascript:OpenCuoWuXX('" + yonghuh + "')\">登记</a>";
                }
                
            }
            return html;
        }
        #endregion

    }
}